﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Rio.Framework.Pages.Manager;
using Rio.Framework.Pages.Workflow.Actions;
using Rio.Framework.WCFService.RioWcfServices;
using System.Linq;
using Rio.Framework.Pages.AppCode.Extensions;

namespace Rio.Framework.Pages.UserControls
{
    public partial class Thumbnail : UserControl
    {
        public String Title
        {
            get { return (String)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty = 
            DependencyProperty.Register("Title", typeof(String), typeof(Thumbnail), null);

        public int NumberPages
        {
            get { return (int)GetValue(NumberPagesProperty); }
            set { SetValue(NumberPagesProperty, value); }
        }
        public static readonly DependencyProperty NumberPagesProperty = 
            DependencyProperty.Register("NumberPages", typeof(int), typeof(Thumbnail), null);

        public BitmapImage ImageSource
        {
            get { return (BitmapImage)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(Thumbnail), null);

        public BitmapImage Icon
        {
            get { return (BitmapImage)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public static readonly DependencyProperty IconProperty = 
            DependencyProperty.Register("Icon", typeof(BitmapImage), typeof(Thumbnail), null);

        /// <summary>
        /// Is a fulltext image
        /// </summary>
        public bool IsFulltext
        {
            get { return (bool)GetValue(IsFulltextProperty); }
            set
            {
                SetValue(IsFulltextProperty, value);
            }
        }
        public static readonly DependencyProperty IsFulltextProperty =
            DependencyProperty.Register("IsFulltext", typeof(bool), typeof(Thumbnail), new PropertyMetadata(false, IsFulltextValueChanged));

        /// <summary>
        /// Is a cut or pasted image
        /// </summary>
        public bool IsCutedOrCopied
        {
            get { return (bool)GetValue(IsCutedOrCopiedProperty); }
            set 
            {
                SetValue(IsCutedOrCopiedProperty, value);
            }
        }
        public static readonly DependencyProperty IsCutedOrCopiedProperty =
            DependencyProperty.Register("IsCutedOrCopied", typeof(bool), typeof(Thumbnail), new PropertyMetadata(false, IsCutedOrCopiedValueChanged));

        public WorkflowState WorkflowState
        {
            get { return (WorkflowState)GetValue(WorkflowStateProperty); }
            set { SetValue(WorkflowStateProperty, value); }
        }
        public static readonly DependencyProperty WorkflowStateProperty =
            DependencyProperty.Register("WorkflowState", typeof(WorkflowState), typeof(Thumbnail), new PropertyMetadata(WorkflowState.None, WorkflowStateValueChanged));

        public Thumbnail()
        {
            InitializeComponent();
        }

        static void WorkflowStateValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Thumbnail currentThumbnail = sender as Thumbnail;

            if (currentThumbnail != null)
            {
                currentThumbnail.SetWorkflowStateStyle((WorkflowState)e.NewValue);
            }
        }

        static void IsCutedOrCopiedValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
                VisualStateManager.GoToState(sender as Thumbnail, "CopiedOrPastedState", false);
            else
                VisualStateManager.GoToState(sender as Thumbnail, "DefaultState", false);
        }

        static void IsFulltextValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
                VisualStateManager.GoToState(sender as Thumbnail, "FulltextState", false);
            else
                VisualStateManager.GoToState(sender as Thumbnail, "DefaultState", false);
        }

        public void SetWorkflowStateStyle(WorkflowState newState)
        {
            if (newState != WorkflowState.None)
            {
                border.BorderBrush =
                    new SolidColorBrush(
                        WorkflowManager.WorkflowStatesInfos[
                            (NetBay.Core.BusinessEntities.Workflow.WorkflowState)
                            Enum.Parse(typeof(NetBay.Core.BusinessEntities.Workflow.WorkflowState),
                                       newState.ToString(),
                                       true)].GetColor());
                border.BorderThickness = new Thickness(2);
                rectangle.Visibility = Visibility.Collapsed;
            }
            else
            {
                VisualStateManager.GoToState(this, "DefaultState", false);
            }
        }
    }

    public class NumberPagesToVisibilityConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((int)value > 0)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class WorkflowStateToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            WorkflowState newState = (WorkflowState)value;

            if (newState == WorkflowState.None)
                return null;

            return WorkflowManager.WorkflowStatesInfos[(NetBay.Core.BusinessEntities.Workflow.WorkflowState)Enum.Parse(typeof(NetBay.Core.BusinessEntities.Workflow.WorkflowState), newState.ToString(), true)].GetIcon();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
